﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Teradata.Client.Provider;
using System.Collections;

using System.Data;

using System.IO;

using System.Web.Security;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class FilteredList : System.Web.UI.Page
{

    private ArrayList alRowFilterColumnNames;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
          
          ShowGrid("", "");
          if (Session["SortExpression"] != null)
                GridView1.Sort(Session["SortExpression"].ToString(),SortDirection.Descending);
 
        }
    }

    private void ShowGrid(string psSortBy, string psSortDir)
    {
        string sGrouping = Request.QueryString["Grouping"];
        string sName = Session["PropertiesForName"] == null ? "" : Session["PropertiesForName"].ToString();

        DataView dv;
        Label1.Width = GridView1.Width;
        Label1.Text = (new Menus()).FilterLegend(sGrouping, sName);
        if (sGrouping == "Properties")
        {
            GridView1.Columns[2].Visible = false;
            dv = (DataView)Session["SecondLevelList.DataSource"];
        }
        else
        {
            GridView1.Columns[1].Visible = false;
            dv = (new Menus()).dvACC_DATA(sGrouping);
        }
        if (psSortBy.Length > 0) dv.Sort = psSortBy + " " + psSortDir;
        GridView1.DataSource = dv;
        GridView1.DataBind();
        if (dv != null)
            Label1.Text = dv.Count == 0 ? "No " : dv.Count.ToString("#,### ") + Label1.Text;

        alRowFilterColumnNames = (ArrayList)Session["alRowFilterColumnNames"];
        if (alRowFilterColumnNames != null)
        {

            // Display the filter columns header in bold white.
            for (int i = 0; i < alRowFilterColumnNames.Count; i++)
            {
                string sColName = ((string)alRowFilterColumnNames[i]).Replace(" ", "_");
                for (int j = 0; j < GridView1.Columns.Count; j++)
                {
                    if (sColName.ToUpper() == ((System.Web.UI.WebControls.BoundField)(GridView1.Columns[j])).DataField.ToUpper()) //(((System.Web.UI.WebControls.DataControlFieldCell)(GridView1.Columns[j])).ContainingField).SortExpression.ToUpper())
                    {
                        GridView1.Columns[j].HeaderStyle.Font.Bold = true;
                        GridView1.Columns[j].HeaderStyle.ForeColor = System.Drawing.Color.FromName("white");
                        break;
                    }

                }

                for (int j = 0; j < GridView1.Columns.Count; j++)
                {
                    if (psSortBy.ToUpper() == ((System.Web.UI.WebControls.BoundField)(GridView1.Columns[j])).DataField.ToUpper()) //(((System.Web.UI.WebControls.DataControlFieldCell)(GridView1.Columns[j])).ContainingField).SortExpression.ToUpper())
                    {
                        GridView1.Columns[j].HeaderStyle.Font.Bold = true;
                        GridView1.Columns[j].HeaderStyle.ForeColor = System.Drawing.Color.FromName("#e5fac9");
                        break;
                    }

                }
            }



        }

    }



    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sDirection = string.Empty;
        switch (e.SortExpression)
        {
            case "MARKET":
            case "State_CD":
            case "Portfolio_NM":
            case "Property_NM":
                break;
            default:
                sDirection = e.SortDirection.ToString() == "Ascending" ? "Desc" : "";
                break;
        }

        ShowGrid(e.SortExpression, sDirection);
        Session["SortExpression"] = e.SortExpression;
    }


    protected void GridView1_RowDatabound(Object sender, GridViewRowEventArgs e)
    {
        alRowFilterColumnNames = (ArrayList)Session["alRowFilterColumnNames"];

        if (alRowFilterColumnNames != null)
        {

            // Display the filter columns value in bold ATT Blue.
            for (int i = 0; i < alRowFilterColumnNames.Count; i++)
            {
                string sColName = ((string)alRowFilterColumnNames[i]).Replace(" ", "_");
                for (int j = 0; j < e.Row.Cells.Count; j++)
                {
                    if (sColName.ToUpper() == (((System.Web.UI.WebControls.DataControlFieldCell)(e.Row.Cells[j])).ContainingField).SortExpression.ToUpper())
                    {
                        e.Row.Cells[j].Style["font-weight"] = "bold";
                        e.Row.Cells[j].Style["color"] = "#0A94D6";
                        break;
                    }

                }
            }


        }
    }

}